package com.lln.controller;

import com.lln.pojo.DeviceType;
import com.lln.pojo.Result;
import com.lln.service.DeviceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("device/type")
public class DeviceTypeController{
    @Autowired
    private DeviceTypeService deviceTypeService;
    @GetMapping
    public Result findAll(){
        List<DeviceType> deviceTypes = deviceTypeService.list();
        return Result.success("查询成功", deviceTypes);
    }
    @PostMapping
    public Result add(@RequestBody DeviceType deviceType){
        deviceType.setCreateId(1);
        if (deviceTypeService.save(deviceType)){
            return Result.success("添加成功", deviceType);
        }else{
            return Result.failed("添加失败");
        }
    }
    @PutMapping
    public Result update(@RequestBody DeviceType deviceType){
        if (deviceTypeService.updateById(deviceType)){
            return Result.success("修改成功", deviceType);
        }else{
            return Result.failed("修改失败");
        }
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        DeviceType deviceType = deviceTypeService.getById(id);
        if (deviceType == null) {
            return Result.failed("该设备类型不存在");
        }
        return Result.success(deviceType);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        if (deviceTypeService.removeById(id)){
            return Result.success("删除成功");
        }else{
            return Result.failed("删除失败");
        }
    }
}
